package dyyx;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class Utils {
	
	private static final int BUF_SIZE = 512;
	
	private static final Random rand = new Random();
	
	
	
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
	
	
	public static String getTimeString(){
		return dtf.format(LocalDateTime.now());
	}
	
	public static void doSleep(long sleep){
		if(sleep<=0){
			return;
		}
		try{
			Thread.sleep(sleep);
		}catch(Throwable e){
			//
		}
	}

	
	
	public static int getRandomInt(int bound){
		return rand.nextInt(bound);
	}

	public static void close(Closeable obj) {
		if (obj == null) {
			return;
		}
		try {
			obj.close();
		} catch (Throwable e) {

		}
	}
	
	
	public static byte[] toBytes(InputStream is) throws Exception {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buf = new byte[BUF_SIZE];
		int len = 0;
		while ((len = is.read(buf)) != -1) {
			if (len > 0) {
				bos.write(buf, 0, len);
			}
		}
		return bos.toByteArray();
	}
}
